﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.Controls
{
    [Serializable]
    public partial class PrintVouchertListCtrl : BaseListControl, IListControl
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        public event ProductNameClickEventHandler ProductNameClick;

        protected void OnProductNameClick(EventArgs e)
        {
            if (ProductNameClick != null)
                ProductNameClick(this, e);
        }

        public View_Vouchers VoucherList
        {
            get { return (View_Vouchers)SourceObject; }
            set { SourceObject = value; }
        }

        protected override void grid_OnItemDataBound(object sender, DataGridItemEventArgs e)
        {
        }

        #region IListControl Members

        public void DeleteCheckedItems()
        {
            String checkedItemID = "";
            for (int i = 0; i < grid.Items.Count; i++)
            {
                DataGridItem item = grid.Items[i];
                Control ctrl = item.FindControl("cbSelect");
                if (ctrl != null && ctrl is CheckBox && ((CheckBox)ctrl).Checked && ((CheckBox)ctrl).Attributes["ItemID"] != null)
                    checkedItemID += "'" + ((CheckBox)ctrl).Attributes["ItemID"].ToString() + "',";
            }
            if (checkedItemID != "")
            {
                checkedItemID = checkedItemID.Trim(',');
                Vouchers.DeleteByIds(checkedItemID);
            }
        }

        public void DeleteSelectedItem()
        {
            throw new NotImplementedException();
        }

        public void AddNewItem(MyGeneration.dOOdads.SqlClientEntity newItem)
        {
            throw new NotImplementedException();
        }

        public void UpdateItem(MyGeneration.dOOdads.SqlClientEntity updatedItem)
        {
            throw new NotImplementedException();
        }

        #endregion

        protected override void grid_OnItemCreated(Object sender, DataGridItemEventArgs e)
        {
            DataGrid grid = sender as DataGrid;
            if (e.Item.ItemType == ListItemType.Header)
            {
                e.Item.Attributes.Add("class", "CustomGridHeader");
                if (grid.AutoGenerateColumns == false)
                {
                    for (int i = 0; i < e.Item.Cells.Count; i++)
                    {
                        DataGridColumn col = (DataGridColumn)grid.Columns[i];
                        if (col.SortExpression == SortExpression && SortExpression != String.Empty)
                        {
                            Image img = new Image();
                            img.CssClass = "SortImage";
                            if (SortDirection == SORT_ASC)
                            {
                                img.ImageUrl = "images/icon_arrow_up.gif";
                                e.Item.Cells[i].Controls.Add(img);
                            }
                            else if (SortDirection == SORT_DESC)
                            {
                                img.ImageUrl = "images/icon_arrow_down.gif";
                                e.Item.Cells[i].Controls.Add(img);
                            }
                        }
                    }
                    Control ctrl = e.Item.FindControl("cbSelectAll");
                    if (ctrl != null)
                        ((CheckBox)ctrl).Attributes.Add("onclick", String.Format("selectAll{0}(this)", ClientID));
                }
                else
                {
                    for (int i = 0; i < e.Item.Cells.Count; i++)
                    {
                        e.Item.Cells[i].Style.Add("text-align", "left");
                        e.Item.Cells[i].Text = Utilities.CamelStringSplitter(e.Item.Cells[i].Text);
                    }
                }
            }
            else if (e.Item.ItemType == ListItemType.Item)
            {
                if (grid.DataKeys.Count > 0)
                    e.Item.Attributes.Add("id", string.Format("{0}_row_{1}", ClientID, grid.DataKeys[e.Item.ItemIndex]));

                if (grid.DataKeys.Count > 0 && grid.DataKeys[e.Item.ItemIndex].ToString() == hidSelectedID.Value)
                    e.Item.Attributes.Add("class", "CustomGridSelectedItem");
                else
                    e.Item.Attributes.Add("class", "CustomGridItem");
                e.Item.Attributes.Add("orgclass", "CustomGridItem");

                if (grid.DataKeys.Count > 0)
                    e.Item.Attributes.Add("ItemID", grid.DataKeys[e.Item.ItemIndex].ToString());
                if (AllowSelecting)
                {
                    if (OnDblClick.Trim() != "")
                        e.Item.Attributes.Add("ondblclick", OnDblClick);
                    if (OnClientClick.Trim() != "")
                        e.Item.Attributes.Add("onclick", String.Format("onItemClick{0}(this);{1}", ClientID, OnClientClick));
                    else
                        e.Item.Attributes.Add("onclick", String.Format("onItemClick{0}(this)", ClientID));
                }
                Control ctrl = e.Item.FindControl("cbSelect");
                if (grid.DataKeys.Count > 0 && grid.DataKeys[e.Item.ItemIndex].ToString() == hidSelectedID.Value)
                    ((CheckBox)ctrl).Checked = true;
                else
                    ((CheckBox)ctrl).Checked = false;
                if (ctrl != null)
                    ((CheckBox)ctrl).Attributes.Add("onclick", String.Format("clickOnCheckBox{0}(this);", ClientID));
            }
            else if (e.Item.ItemType == ListItemType.AlternatingItem)
            {
                if (grid.DataKeys.Count > 0)
                    e.Item.Attributes.Add("id", string.Format("{0}_row_{1}", ClientID, grid.DataKeys[e.Item.ItemIndex]));
                if (grid.DataKeys.Count > 0 && grid.DataKeys[e.Item.ItemIndex].ToString() == hidSelectedID.Value)
                    e.Item.Attributes.Add("class", "CustomGridSelectedItem");
                else
                    e.Item.Attributes.Add("class", "CustomGridItem");
                e.Item.Attributes.Add("orgclass", "CustomGridItem");
                if (grid.DataKeys.Count > 0)
                    e.Item.Attributes.Add("ItemID", grid.DataKeys[e.Item.ItemIndex].ToString());
                if (AllowSelecting)
                {
                    if (OnDblClick.Trim() != "")
                        e.Item.Attributes.Add("ondblclick", OnDblClick);
                    if (OnClientClick.Trim() != "")
                        e.Item.Attributes.Add("onclick", String.Format("onItemClick{0}(this);{1}", ClientID, OnClientClick));
                    else
                        e.Item.Attributes.Add("onclick", String.Format("onItemClick{0}(this)", ClientID));
                }
                Control ctrl = e.Item.FindControl("cbSelect");
                if (grid.DataKeys.Count > 0 && grid.DataKeys[e.Item.ItemIndex].ToString() == hidSelectedID.Value)
                    ((CheckBox)ctrl).Checked = true;
                else
                    ((CheckBox)ctrl).Checked = false;
                if (ctrl != null)
                    ((CheckBox)ctrl).Attributes.Add("onclick", String.Format("clickOnCheckBox{0}(this);", ClientID));
            }
            else if (e.Item.ItemType == ListItemType.Pager)
            {
                e.Item.Attributes.Add("class", "CustomGridPager");
            }
            else if (e.Item.ItemType == ListItemType.Footer)
            {
                e.Item.Attributes.Add("class", "CustomGridFooter");
            }
            //else if (e.Item.ItemType == ListItemType.SelectedItem)
            //{
            //    e.Item.Attributes.Add("class", "CustomGridSelectedItem");
            //    Control ctrl = e.Item.FindControl("cbSelect");
            //    if (ctrl != null)
            //        ((CheckBox)ctrl).Checked = true;
            //}
        }
    }
}